﻿package plugin.superstore.game
{
    import __AS3__.vec.*;
    import com.core.*;
    import com.sh.game.util.*;
    import flash.external.*;
    import flash.geom.*;
    import flash.utils.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.store.*;
    import plugin.superstore.*;
    import uiresource.store.*;
    import view.uilt.*;
    import vo.*;

    public class XunBaoShopGame extends XunbaoShopUI
    {
        public var showType:int = 2;
        private var _instance:XunBaoShop;
        private var _selectRoot:int = 0;
        private var _scList:XunbaoShopList;
        private var _moneyArr:Array;
        private var cfgStore:Array;
        private var cfgShow:Array;
        private var angleArr:Array;
        private var titleArr:Array;
        private var isInit:Boolean = false;
        private var msgArr:Array;
        public var showIndex:int = 0;
        public var itemCount:int = 7;
        public var txtArr:Vector.<Label>;
        public var firstTxt:Label;
        public var maxIndex:int;
        public var changeNext:int = -1;

        public function XunBaoShopGame(param1:XunBaoShop)
        {
            this.cfgStore = [];
            this.cfgShow = [];
            this.angleArr = [];
            this.titleArr = [];
            this.msgArr = ["<font color =\'#4ba0de\'>布拉格@神话</font> 刚刚兑换了 <font color=\'#e19f21\'>金砖(大)</font> 又变强了", "<font color =\'#4ba0de\'>给我一支烟</font> 刚刚兑换了 <font color=\'#e19f21\'>宝石结晶(大)</font> 又变强了", "<font color =\'#4ba0de\'>给我一支烟</font> 刚刚兑换了 <font color=\'#e19f21\'>宝石结晶(大)</font> 又变强了", "<font color =\'#4ba0de\'>负天下¤炎彬</font> 刚刚兑换了 <font color=\'#e19f21\'>宝石结晶(小)</font> 又变强了", "<font color =\'#4ba0de\'>负天下¤炎彬</font> 刚刚兑换了 <font color=\'#e19f21\'>宝石结晶(小)</font> 又变强了", "<font color =\'#4ba0de\'>猪头无敌</font> 刚刚兑换了 <font color=\'#e19f21\'>魂珠结晶(中)</font> 又变强了", "<font color =\'#4ba0de\'>猪头无敌</font> 刚刚兑换了 <font color=\'#e19f21\'>魂珠结晶(小)</font> 又变强了"];
            this.txtArr = new Vector.<Label>;
            this._instance = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            var _loc_7:Array = null;
            var _loc_8:Object = null;
            var _loc_9:Control = null;
            var _loc_10:Array = null;
            var _loc_11:Object = null;
            var _loc_12:Object = null;
            var _loc_1:* = ConfigDictionary.data.storeMsg[this.showType];
            var _loc_2:* = ConfigDictionary.data.item;
            var _loc_3:* = UserData.getUserData("career");
            var _loc_4:* = UserData.getUserData("sex");
            var _loc_5:* = ModelProxy.getValue("data", "hefu");
            var _loc_6:* = int(ModelProxy.getValue("data", "hefuday")) + 1;
            for (_loc_8 in _loc_1)
            {
                
                _loc_10 = _loc_1[_loc_8];
                this.cfgStore[_loc_8] = [];
                for each (_loc_11 in _loc_10)
                {
                    
                    _loc_12 = ItemObject.create(_loc_2[_loc_11.itemid]);
                    if (_loc_12)
                    {
                        if (_loc_12.career > 0)
                        {
                            if (_loc_12.career != _loc_3)
                            {
                                continue;
                            }
                        }
                        if (_loc_8 != 3)
                        {
                            if (_loc_12.sex != 2)
                            {
                                if (_loc_12.sex != _loc_4)
                                {
                                    continue;
                                }
                            }
                        }
                        if (_loc_11.starttime)
                        {
                            _loc_7 = _loc_11.starttime.split("#");
                            switch(int(_loc_7[0]))
                            {
                                case 4:
                                {
                                    if (!_loc_5)
                                    {
                                        break;
                                    }
                                    if (_loc_6 < _loc_7[1])
                                    {
                                        break;
                                    }
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                        }
                        if (_loc_11.endtime)
                        {
                            _loc_7 = _loc_11.endtime.split("#");
                            switch(int(_loc_7[0]))
                            {
                                case 4:
                                {
                                    if (!_loc_5)
                                    {
                                        break;
                                    }
                                    if (_loc_6 > _loc_7[1])
                                    {
                                        break;
                                    }
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                    this.cfgStore[_loc_8].push({storeMsg:_loc_11, itemMsg:_loc_12});
                }
            }
            _loc_9 = Control.create(StoreControl, this);
            ModelProxy.addObserver("store", _loc_9);
            this.initEvent();
            this.showRun();
            return;
        }// end function

        private function showRun() : void
        {
            var _loc_3:String = null;
            var _loc_4:Label = null;
            var _loc_1:int = 0;
            this.maxIndex = this.msgArr.length;
            var _loc_2:int = 0;
            while (_loc_2 < this.itemCount)
            {
                
                _loc_3 = this.msgArr[_loc_2];
                _loc_4 = new Label();
                _loc_4.stroke = "0x0";
                _loc_4.color = "0xffe4c7";
                _loc_4.isHtml = true;
                _loc_4.text = _loc_3;
                _loc_4.width = 212;
                _loc_4.multiline = true;
                _loc_4.wordWrap = true;
                _loc_4.leading = 4;
                _loc_4.mouseChildren = false;
                _loc_4.mouseEnabled = false;
                box_run.addChild(_loc_4);
                _loc_4.y = _loc_1;
                _loc_1 = _loc_1 + (_loc_4.height + 5);
                this.txtArr.push(_loc_4);
                _loc_2++;
            }
            this.firstTxt = this.txtArr.shift();
            this.showIndex = this.itemCount;
            box_run.scrollRect = new Rectangle(0, 0, box_run.width, box_run.height);
            setInterval(this.doRun, 40);
            return;
        }// end function

        private function doRun() : void
        {
            var _loc_2:int = 0;
            var _loc_3:Label = null;
            var _loc_4:Label = null;
            var _loc_1:* = box_run.scrollRect;
            (_loc_1.y + 1);
            if (this.changeNext > 0)
            {
                var _loc_5:String = this;
                var _loc_6:* = this.changeNext + 1;
                _loc_5.changeNext = _loc_6;
                if (this.changeNext > 5)
                {
                    _loc_1.y = 0;
                    _loc_2 = 0;
                    this.firstTxt.y = _loc_2;
                    _loc_2 = _loc_2 + (this.firstTxt.height + 5);
                    for each (_loc_3 in this.txtArr)
                    {
                        
                        _loc_3.y = _loc_2;
                        _loc_2 = _loc_2 + (_loc_3.height + 5);
                    }
                    this.changeNext = -1;
                }
            }
            if (this.firstTxt.y + this.firstTxt.height <= _loc_1.y)
            {
                _loc_4 = this.txtArr.pop();
                this.firstTxt.y = _loc_4.y + _loc_4.height + 5;
                this.txtArr.push(_loc_4, this.firstTxt);
                this.firstTxt = this.txtArr.shift();
                var _loc_5:String = this;
                var _loc_6:* = this.showIndex + 1;
                _loc_5.showIndex = _loc_6;
                if (this.showIndex >= this.maxIndex)
                {
                    this.changeNext = 0;
                }
            }
            box_run.scrollRect = _loc_1;
            return;
        }// end function

        public function init() : void
        {
            switch(this.showType)
            {
                case 2:
                {
                    shangdian.text = "寻宝积分兑换";
                    break;
                }
                case 3:
                {
                    shangdian.text = "行会贡献兑换";
                    break;
                }
                default:
                {
                    break;
                }
            }
            var _loc_1:* = ConfigDictionary.data.superStore;
            var _loc_2:* = _loc_1[this.showType];
            var _loc_3:* = _loc_2.labels.split("#");
            var _loc_4:* = _loc_2.types.split("#");
            var _loc_5:int = 0;
            while (_loc_5 < _loc_3.length)
            {
                
                this.titleArr.push({label:_loc_3[_loc_5], type:_loc_4[_loc_5]});
                _loc_5++;
            }
            list_root.array = this.titleArr;
            list_root.renderHandler = new Handler(this.rootRender);
            this._moneyArr = [];
            if (_loc_2.moneyType)
            {
                this._moneyArr = _loc_2.moneyType.split("#");
            }
            txt_score.text = this.getMoneyMsg(this._moneyArr[0]).count + "";
            return;
        }// end function

        private function recharge() : void
        {
            if (!ItemUilt.canpay)
            {
                ModelProxy.change("movetips", "error", "功能尚未开放，敬请期待");
                return;
            }
            ExternalInterface.call("topay");
            return;
        }// end function

        private function rootRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Button = null;
            if (param2 < list_root.length)
            {
                _loc_3 = param1.dataSource;
                _loc_4 = param1.getChildByName("btn") as Button;
                _loc_4.label = _loc_3.label;
                if (param2 == this._selectRoot)
                {
                    _loc_4.selected = true;
                    if (!this._scList)
                    {
                        this._scList = new XunbaoShopList();
                        this.addChild(this._scList);
                    }
                    this.checkAngle();
                    this._scList.showList(this.cfgShow[list_root.array[this._selectRoot].type]);
                }
                _loc_4.clickHandler = new Handler(this.rootClick, [param2]);
            }
            return;
        }// end function

        private function rootClick(param1:int) : void
        {
            this.setSelect(list_root, this._selectRoot, false);
            this.setSelect(list_root, param1, true);
            this._selectRoot = param1;
            this.checkAngle();
            this._scList.showList(this.cfgShow[list_root.array[this._selectRoot].type]);
            return;
        }// end function

        private function setSelect(param1:List, param2:int, param3:Boolean) : void
        {
            var _loc_4:* = param1.getCell(param2);
            var _loc_5:* = param1.getCell(param2).getChildByName("btn") as Button;
            (param1.getCell(param2).getChildByName("btn") as Button).selected = param3;
            return;
        }// end function

        private function initEvent() : void
        {
            box_base.btn_close.clickHandler = new Handler(this._instance.close);
            link_xunbao.clickHandler = new Handler(this.toXunbao);
            return;
        }// end function

        private function toXunbao() : void
        {
            ModelProxy.change("plugin", "open", {target:"xunbao"});
            return;
        }// end function

        public function update(param1:int, param2:Object) : void
        {
            switch(param1)
            {
                case StorePublic._1INITSTORE:
                {
                    this.init();
                    this.isInit = true;
                    break;
                }
                case StorePublic._2REFRESH:
                {
                    if (this.visible)
                    {
                        if (this.isInit)
                        {
                            txt_score.text = this.getMoneyMsg(this._moneyArr[0]).count + "";
                            this.checkAngle();
                            this._scList.showList(this.cfgShow[list_root.array[this._selectRoot].type]);
                        }
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function checkAngle() : void
        {
            var _loc_1:Object = null;
            var _loc_2:Array = null;
            var _loc_3:Array = null;
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_6:Object = null;
            var _loc_7:String = null;
            var _loc_8:Boolean = false;
            var _loc_9:int = 0;
            var _loc_10:Array = null;
            var _loc_11:Boolean = false;
            var _loc_12:int = 0;
            var _loc_13:Array = null;
            var _loc_14:Object = null;
            var _loc_15:int = 0;
            var _loc_16:Box = null;
            var _loc_17:Image = null;
            var _loc_18:Label = null;
            this.cfgShow = [];
            this.angleArr = [];
            for (_loc_1 in this.cfgStore)
            {
                
                _loc_3 = this.cfgStore[_loc_1];
                this.cfgShow[_loc_1] = [];
                this.angleArr[_loc_1] = 0;
                for each (_loc_4 in _loc_3)
                {
                    
                    if (_loc_4.storeMsg.request)
                    {
                        _loc_10 = _loc_4.storeMsg.request.split("&");
                        _loc_11 = false;
                        _loc_12 = 0;
                        while (_loc_12 < _loc_10.length)
                        {
                            
                            if (this.checkCondition(_loc_10[_loc_12]))
                            {
                                _loc_11 = true;
                                break;
                            }
                            _loc_12++;
                        }
                        if (_loc_11)
                        {
                            continue;
                        }
                    }
                    _loc_5 = ConfigDictionary.data.item;
                    _loc_6 = ItemObject.create(_loc_5[_loc_4.storeMsg.costtype]);
                    _loc_7 = _loc_4.storeMsg.nowprice + _loc_6.name;
                    _loc_8 = false;
                    if (_loc_4.storeMsg.needitems)
                    {
                        _loc_13 = _loc_4.storeMsg.needitems.split("#");
                        _loc_14 = ItemObject.create(_loc_5[_loc_13[0]]);
                        _loc_7 = _loc_7 + (" + " + "<font color=\'" + MColor.getqualitycolor(_loc_4.itemMsg.quality) + "\'>" + _loc_14.name + (_loc_13[1] > 1 ? ("*" + _loc_13[1]) : ("")) + "</font>");
                        _loc_8 = ItemUilt.getAllCount(_loc_13[0]) < _loc_13[1];
                    }
                    _loc_4.cost = _loc_7;
                    _loc_9 = this.getMoney(_loc_4.storeMsg.costtype);
                    if (_loc_4.storeMsg.nowprice > _loc_9)
                    {
                        _loc_4.state = 1;
                    }
                    else if (_loc_8)
                    {
                        _loc_4.state = 2;
                    }
                    else
                    {
                        var _loc_23:* = this.angleArr;
                        var _loc_24:* = _loc_1;
                        var _loc_25:* = this.angleArr[_loc_1] + 1;
                        _loc_23[_loc_24] = _loc_25;
                        _loc_4.state = 3;
                    }
                    this.cfgShow[_loc_1].push(_loc_4);
                }
            }
            for (_loc_1 in this.angleArr)
            {
                
                if (_loc_1 == 1 || _loc_1 == 2 || _loc_1 == 4)
                {
                    _loc_15 = 0;
                    while (_loc_15 < list_root.array.length)
                    {
                        
                        if (list_root.array[_loc_15].type == _loc_1)
                        {
                            _loc_16 = list_root.getCell(_loc_15);
                            _loc_17 = _loc_16.getChildByName("angleBg") as Image;
                            _loc_18 = _loc_16.getChildByName("angleCount") as Label;
                            if (this.angleArr[_loc_1] > 0)
                            {
                                _loc_17.visible = true;
                                _loc_18.visible = true;
                                _loc_18.text = this.angleArr[_loc_1];
                            }
                            else
                            {
                                _loc_17.visible = false;
                                _loc_18.visible = false;
                            }
                            break;
                        }
                        _loc_15++;
                    }
                }
            }
            _loc_2 = [];
            for (_loc_1 in this.cfgShow)
            {
                
                if (this.cfgShow[_loc_1].length > 0)
                {
                    _loc_2.push(this.titleArr[(int(_loc_1) - 1)]);
                }
            }
            if (_loc_2.length != list_root.array.length)
            {
                list_root.array = _loc_2;
            }
            return;
        }// end function

        private function getMoneyMsg(param1:int) : Object
        {
            var _loc_2:String = null;
            var _loc_3:int = 0;
            switch(param1)
            {
                case 3:
                {
                    _loc_2 = "png.common.cs2_yuanbao";
                    _loc_3 = UserData.getBagData("money");
                    break;
                }
                case 6:
                {
                    _loc_2 = "png.common.cs2_jinbi";
                    _loc_3 = UserData.getBagData("gold");
                    break;
                }
                case 1:
                {
                    _loc_2 = "png.common.bgold";
                    _loc_3 = UserData.getBagData("bgold");
                    break;
                }
                case 2:
                {
                    _loc_2 = "png.common.cs2_qian";
                    _loc_3 = UserData.getBagData("bonus");
                    break;
                }
                case 15:
                {
                    _loc_2 = "png.common.jifen";
                    _loc_3 = UserData.getBagData("jifen");
                    break;
                }
                default:
                {
                    _loc_2 = "png.common.jifen";
                    break;
                    break;
                }
            }
            return {url:_loc_2, count:_loc_3};
        }// end function

        private function checkCondition(param1:String) : Boolean
        {
            var _loc_2:Array = null;
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            _loc_2 = param1.split("#");
            switch(int(_loc_2[0]))
            {
                case 0:
                {
                    break;
                }
                case 1:
                {
                    _loc_3 = ModelProxy.getValue("data", "achievement");
                    _loc_4 = _loc_3["a" + _loc_2[1]];
                    if (_loc_4 && _loc_4[1] > 0)
                    {
                        return false;
                    }
                    break;
                }
                case 2:
                {
                    _loc_3 = ModelProxy.getValue("data", "achievement");
                    if (_loc_3.res >= _loc_2[1])
                    {
                        return false;
                    }
                    break;
                }
                case 3:
                {
                    _loc_5 = UserData.getUserData("level");
                    if (_loc_5 >= _loc_2[1])
                    {
                        return false;
                    }
                    break;
                }
                case 13:
                {
                    _loc_6 = UserData.getUserData("rein");
                    if (_loc_6 >= _loc_2[1])
                    {
                        return false;
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return true;
        }// end function

        private function getMoney(param1:int) : Number
        {
            var _loc_2:* = UserData.getBagData();
            var _loc_3:Number = 0;
            switch(param1)
            {
                case 1:
                {
                    _loc_3 = _loc_2.bgold;
                    break;
                }
                case 3:
                {
                    _loc_3 = _loc_2.money;
                    break;
                }
                case 2:
                {
                    _loc_3 = _loc_2.bonus;
                    break;
                }
                case 5:
                {
                    _loc_3 = _loc_2.contribution;
                    break;
                }
                case 6:
                {
                    _loc_3 = _loc_2.gold;
                    break;
                }
                case 9:
                {
                    _loc_3 = _loc_2.zuoqi;
                    break;
                }
                case 10:
                {
                    _loc_3 = _loc_2.yuyi;
                    break;
                }
                case 11:
                {
                    _loc_3 = _loc_2.shengwang;
                    break;
                }
                case 15:
                {
                    _loc_3 = _loc_2.jifen;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_3;
        }// end function

    }
}
